package com.xmy.cultivate.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.annotaion.ApiMethod;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.response.ArrivalRate;
import com.xmy.cultivate.entity.response.MathChaPlan;
import com.xmy.cultivate.entity.response.PassRade;
import com.xmy.cultivate.entity.response.PassRadeDetail;
import com.xmy.cultivate.entity.views.FenAdminData;
import com.xmy.cultivate.entity.views.LeadDataRs;
import com.xmy.cultivate.entity.views.StudyPlan;
import com.xmy.cultivate.feign.IPlanFeignClient;
import com.xmy.cultivate.feign.study_admin.IStudyAdminFeignClient;
import com.xmy.cultivate.mapper.StudentMapper;
import com.xmy.cultivate.respons.EduTask;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/studyPlan")
public class StudyPlanController extends BaseController {

    @Autowired
    @Lazy
    IStudyPlanService iStudyPlanService;

    @Autowired
    @Lazy
    ICourseTypeService iCourseTypeService;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @Autowired
    @Lazy
    IDictService iDictService;

    @Autowired
    @Lazy
    IStudyAdminFeignClient iStudyAdminFeignClient;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    ISchoolQuarterService iSchoolQuarterService;

    @Autowired
    IOrganizationService iOrganizationService;

    @Autowired
    IGradeService iGradeService;

    @Autowired
    IRecordCourseService iRecordCourseService;

    @Autowired
    IAuthAdminContentService iAuthAdminContentService;
    ;

    /**
     * 获取学习进度
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getPlanList")
    public Result getPlanList(@RequestParam Map<String, String> reMap,
                              @RequestParam(value = "schoolId", defaultValue = "", required = false) String schoolId,
                              @RequestParam(value = "lessonType", defaultValue = "1", required = false) String lessonType) {

        //Integer type, Integer quarterNum, String startDate, String endDate, String trackId

        String yearPart = reMap.get("yearPart");

        String yearClassId = reMap.get("yearClassId");

        String classTypeId = reMap.get("classTypeId");

        String subjectsId = "1";

        String startDate = null;

        String endDate = null;

        String trackId = null;

        String quarterNum = reMap.get("quarterNum");

        String gradeSection = reMap.get("gradeSection");

        Integer type = 1;
        if (reMap.get("type") != null) {
            type = Integer.parseInt(reMap.get("type"));
        }

        List<StudyPlan> studyPlanList = iStudyPlanService.getPlanList(schoolId, yearPart, yearClassId, type, classTypeId, subjectsId, quarterNum, startDate, endDate, trackId, gradeSection, lessonType);
        return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
    }

    /**
     * 获取学习进度详情
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getPlanDetail")
    public Result getPlanDetail(@RequestParam Map<String, String> reMap,
                                @RequestParam(value = "lessonType", defaultValue = "1", required = false) String lessonType) {

        //Integer type, Integer quarterNum, String startDate, String endDate, String trackId

        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 20 : Integer.valueOf((String) reMap.get("pageSize"));

        String schoolId = reMap.get("schoolId");

        String teacherId = reMap.get("teacherId");

        String gradeId = reMap.get("gradeId");

        String yearPart = reMap.get("yearPart");

        String yearClassId = reMap.get("yearClassId");

        String classTypeId = reMap.get("classTypeId");

        String subjectsId = "1";

        String startDate = null;

        String endDate = null;

        String trackId = null;

        String quarterNum = reMap.get("quarterNum");

        String gradeSection = reMap.get("gradeSection");

        Integer type = 1;
        if (reMap.get("type") != null) {
            type = Integer.parseInt(reMap.get("type"));
        }

        IPage<StudyPlan> iPage = iStudyPlanService.getPlanDetail(schoolId, teacherId, gradeId, yearPart, yearClassId, type, classTypeId, subjectsId, quarterNum, startDate, endDate, trackId, gradeSection, lessonType, pageNum, pageSize);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    /**
     * 获取升班率数据
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getGoUpStudentList")
    public Result getGoUpStudentList(@RequestParam Map<String, String> reMap,
                                     @RequestParam(value = "schoolId", defaultValue = "", required = false) String schoolId) {

        //Integer type, Integer quarterNum, String startDate, String endDate, String trackId

        String yearPart = reMap.get("yearPart");

        String yearClassId = reMap.get("yearClassId");

        String classTypeId = reMap.get("classTypeId");

        String subjectsId = "1";

        String startDate = null;

        String endDate = null;

        String trackId = null;

        String quarterNum = reMap.get("quarterNum");

        String gradeSection = reMap.get("gradeSection");

        Integer type = 1;
        if (reMap.get("type") != null) {
            type = Integer.parseInt(reMap.get("type"));
        }

        List<StudyPlan> studyPlanList = iStudyPlanService.getGoUpList(schoolId, yearPart, yearClassId, type, classTypeId, subjectsId, quarterNum, startDate, endDate, trackId, gradeSection);
        return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
    }

    /**
     * 获取升班详情
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getGoUpStudentDetail")
    public Result getGoUpStudentDetail(@RequestParam Map<String, String> reMap) {

        //Integer type, Integer quarterNum, String startDate, String endDate, String trackId

        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 20 : Integer.valueOf((String) reMap.get("pageSize"));

        pageSize = 10000;

        String schoolId = reMap.get("schoolId");

        String teacherId = reMap.get("teacherId");

        String gradeId = reMap.get("gradeId");

        String yearPart = reMap.get("yearPart");

        String yearClassId = reMap.get("yearClassId");

        String classTypeId = reMap.get("classTypeId");

        String isGoUp = reMap.get("isGoUp");

        String subjectsId = "1";

        String startDate = null;

        String endDate = null;

        String trackId = null;

        String quarterNum = reMap.get("quarterNum");

        String gradeSection = reMap.get("gradeSection");

        Integer type = 1;
        if (reMap.get("type") != null) {
            type = Integer.parseInt(reMap.get("type"));
        }

        List<StudyPlan> studyPlanList = iStudyPlanService.getGoUpDetail(schoolId, teacherId, gradeId, yearPart, yearClassId, type, classTypeId, subjectsId, quarterNum, startDate, endDate, trackId, gradeSection, isGoUp, pageNum, pageSize);
        return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
    }

    /**
     * 教师通关率、教师正确率
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getTeacherRate")
    public Result getTeacherRate(@RequestParam Map<String, String> reMap) {
        String yearPart = reMap.get("yearPart");  //年份
        String quarterNum = reMap.get("quarterNum"); //学期
        String yearClassId = reMap.get("yearClassId"); //年级
        String classTypeId = reMap.get("classTypeId"); //班型
        String taskDateStart = reMap.get("taskDateStart");// 开始日期
        String taskDateEnd = reMap.get("taskDateEnd");  // 结束日期
        Integer taskCourseNum = Integer.parseInt(reMap.get("taskCourseNum"));// 开始课次
        Integer taskCourseNumNow = Integer.parseInt(reMap.get("taskCourseNumNow"));   // 结束课次
        String gradeSection = reMap.get("gradeSection");  //学段
        String tactType = reMap.get("tactType");// 0全部，1复习检测，2学习检测
        Integer type = StringUtils.isEmpty(reMap.get("type")) ? 1 : Integer.valueOf(reMap.get("type"));
        String teamId = reMap.get("teamId");
        String classify = reMap.get("classify");
        String trackType = reMap.get("trackType");
        String listState = reMap.get("listState"); // 0通关率，1正确率
        if (listState == null || listState.isEmpty()) {
            listState = "0";
        }
        String shutnumType = reMap.get("shutnumType");// 1单词，2专项和试卷

//        if (StringUtils.isBlank(reMap.get("taskCourseNumNow")) || StringUtils.isBlank(taskDateStart) || StringUtils.isBlank(taskDateEnd)) {
//            return new Result(Code.OK, null, "无数据");
//        }
        if (StringUtils.isNotBlank(gradeSection)) {
            //年级段获取出年级
            yearClassId = CommonUtil.getSectionYearClassIds(gradeSection);
        }

        List<PassRade> teacherStudyPlanList = iStudyPlanService.getTeacherRate(yearPart, yearClassId, classTypeId, quarterNum, gradeSection, taskCourseNum, taskCourseNumNow, taskDateStart, taskDateEnd, tactType, listState, shutnumType, teamId, classify, trackType, type);


        return new Result(Code.OK, new PageResult(teacherStudyPlanList, (long) teacherStudyPlanList.size(), 1L));
    }


    /**
     * 教师通关率详情、教师正确率详情
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getTeacherRateDetail")
    public Result getTeacherRateDetail(@RequestParam Map<String, String> reMap) {
        String yearPart = reMap.get("yearPart");  //年份
        String quarterNum = reMap.get("quarterNum"); //学期
        String yearClassId = reMap.get("yearClassId"); //年级
        String classTypeId = reMap.get("classTypeId"); //班型
        String taskDateStart = reMap.get("taskDateStart");// 开始日期
        String taskDateEnd = reMap.get("taskDateEnd");  // 结束日期
        Integer taskCourseNum = Integer.parseInt(reMap.get("taskCourseNum"));// 开始课次
        Integer taskCourseNumNow = Integer.parseInt(reMap.get("taskCourseNumNow"));   // 结束课次
        String gradeSection = reMap.get("gradeSection");  //学段
        String tactType = reMap.get("tactType");// 0全部，1复习检测，2学习检测
        Integer type = StringUtils.isEmpty(reMap.get("type")) ? 1 : Integer.valueOf(reMap.get("type"));
        String teamId = reMap.get("teamId");
        String classify = reMap.get("classify");
        String trackType = reMap.get("trackType");
        String listState = reMap.get("listState"); // 0通关率，1正确率
        if (listState == null || listState.isEmpty()) {
            listState = "0";
        }
        String detailType = reMap.get("detailType"); // 1应通关，2已通关，3未通关，4当前时间段通关  5正确 6错误
        String teacherId = reMap.get("teacherId"); //
        Integer pageNum = StringUtils.isEmpty(reMap.get("pageNum")) ? 1 : Integer.valueOf(reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty(reMap.get("pageSize")) ? 10 : Integer.valueOf(reMap.get("pageSize"));

//        if (StringUtils.isBlank(reMap.get("taskCourseNumNow")) || StringUtils.isBlank(taskDateStart) || StringUtils.isBlank(taskDateEnd)) {
//            return new Result(Code.OK, null, "无数据");
//        }
        if (StringUtils.isNotBlank(gradeSection) && StringUtils.isNotBlank(yearClassId)) {
            //年级段获取出年级
            yearClassId = CommonUtil.getSectionYearClassIds(gradeSection);
        }

        //List<PassRadeDetail> teacherRateDetailList = iStudyPlanService.getTeacherRateDetail(pageNum,pageSize,yearPart, yearClassId, classTypeId, quarterNum, gradeSection, taskCourseNum, taskCourseNumNow, taskDateStart, taskDateEnd, tactType, listState, detailType, teacherId);
        Page<PassRadeDetail> iPage = iStudyPlanService.getTeacherRateDetail(pageNum, pageSize, yearPart, yearClassId, classTypeId, quarterNum, gradeSection, taskCourseNum, taskCourseNumNow, taskDateStart, taskDateEnd, tactType, listState, detailType, teacherId, teamId, classify, trackType, type);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
//        return new Result(Code.OK, new PageResult(teacherRateDetailList, (long) teacherRateDetailList.size(), 1L));
    }


    /**
     * 最低进度通关率
     *
     * @param reMap
     * @return
     */
    @ApiMethod(value = "home:english:clearance-rate", description = "英语通关率")
    @GetMapping("/getMinimumTaskProgressRate")
    public Result getMinimumTaskProgressRate(@RequestParam Map<String, String> reMap,
                                             @RequestParam(value = "categoryId", defaultValue = "1", required = false) String categoryId,
                                             @RequestParam(value = "teacherId", defaultValue = "", required = false) String teacherId,
                                             @RequestParam(value = "listState", defaultValue = "0", required = false) String listState, // 0通关率，1正确率
                                             @RequestParam(value = "isHoliday", defaultValue = "0", required = false) Integer isHoliday, // 0默认，1假期
                                             @RequestParam(value = "moldType", required = false, defaultValue = "1") String moldType, // 1正常通关率、2到勤通关率
                                             @RequestParam(value = "isYearClass", required = false, defaultValue = "0") Integer isYearClass // 是否根据年级分组，0否，1是
    ) {
        String yearPart = reMap.get("yearPart");
        String yearClassId = reMap.get("yearClassId");
        String classTypeId = reMap.get("classTypeId");
        String quarterNum = reMap.get("quarterNum");
        String gradeSection = reMap.get("gradeSection");
        Integer type = StringUtils.isEmpty(reMap.get("type")) ? 1 : Integer.valueOf(reMap.get("type"));
        // 人数1，关数2
        Integer tabsType = StringUtils.isEmpty(reMap.get("tabsType")) ? 1 : Integer.valueOf(reMap.get("tabsType"));
        // 包含0，不包含1
        String readingStatus = reMap.get("readingStatus");
        // 班型
        String courseTypeId = reMap.get("courseTypeId");
        // 开始日期
        String taskDateStart = reMap.get("taskDateStart");
        // 结束日期
        String taskDateEnd = reMap.get("taskDateEnd");
        String schoolId = reMap.get("schoolId");
        //0三次课以内，1三次课以外
        String include = reMap.get("include");
        String teamId = reMap.get("teamId");
        String classify = reMap.get("classify");
        String trackType = reMap.get("trackType");
        String tactType = reMap.get("tactType");// 0全部，1复习检测，2学习检测
        String teacherName = reMap.get("teacherName");
        String studentName = reMap.get("studentName");
        String shutnumType = reMap.get("shutnumType");// 1单词，2专项和试卷

        if (StringUtils.isBlank(reMap.get("taskCourseNumNow")) || StringUtils.isBlank(taskDateStart) || StringUtils.isBlank(taskDateEnd)) {
            return new Result(Code.OK, null, "无数据");
        }
        AuthAdminContent authAdminContent = iAuthAdminContentService.getPermissionForTeacherId(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), this.adminId, this.baseSchoolIdStr);
        LeadDataRs leadDataRs = authAdminContent.getLeadDataRs();

        List<PassRade> studyPlanList = new ArrayList<>();
        //个人
        if (authAdminContent.getIsPerson() == 1) {
            //如果type为1或者为2
            if (type != 1 && type != 2) {
                return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
            }
        }

        //不包含英语科目
        if (StringUtils.isBlank(authAdminContent.getCombineSubjectsIds())) {
            return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
        }
        if (!authAdminContent.getCombineSubjectsIds().contains("1")) {
            return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
        }

        // 开始课次
        Integer taskCourseNum = Integer.parseInt(reMap.get("taskCourseNum"));
        // 结束课次
        Integer taskCourseNumNow = Integer.parseInt(reMap.get("taskCourseNumNow"));

        if (StringUtils.isNotBlank(gradeSection)) {
            //年级段获取出年级
            yearClassId = CommonUtil.getSectionYearClassIds(gradeSection);
        }

        if (StringUtils.isBlank(yearClassId)) {
            yearClassId = authAdminContent.getYearClassIds();
        } else {
            String leadYearClassId = iAuthAdminContentService.getIntersectionForStr(leadDataRs.getYearClassIds(), yearClassId);
            leadDataRs.setYearClassIds(leadYearClassId);

            yearClassId = iAuthAdminContentService.getIntersectionForStr(authAdminContent.getYearClassIds(), yearClassId);
        }

        if (StringUtils.isBlank(schoolId)) {
            schoolId = authAdminContent.getSchoolIds();
        } else {
            String leadSchoolId = iAuthAdminContentService.getIntersectionForStr(leadDataRs.getSchoolIds(), schoolId);
            leadDataRs.setSchoolIds(leadSchoolId);

            schoolId = iAuthAdminContentService.getIntersectionForStr(authAdminContent.getSchoolIds(), schoolId);
        }

        if (StringUtils.isNotBlank(teacherId)) {
            if (!teacherId.equals(String.valueOf(leadDataRs.getTeacherId()))) {
                leadDataRs.setTeacherId(0L);
            }
        }

        if (StringUtils.isBlank(teacherId)) {
            if (authAdminContent.getIsPerson() == 1) {
                teacherId = String.valueOf(this.adminId);
            }
        }

        studyPlanList = iStudyPlanService.getMinimumTaskProgressRateNew(Integer.valueOf(readingStatus), schoolId, yearPart, yearClassId, type, classTypeId, quarterNum, gradeSection, taskCourseNum, taskCourseNumNow, courseTypeId, taskDateStart, taskDateEnd, tabsType, include, teamId, classify, trackType, categoryId, teacherId, tactType, listState, teacherName, shutnumType, studentName, isHoliday, moldType, isYearClass, authAdminContent, leadDataRs);

        if (studyPlanList == null) {
            studyPlanList = new ArrayList<>();
            return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
        }

        if (type == 6) {
            List<PassRade> collectList = new ArrayList<>();

            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("year_part", yearPart);
            queryWrapper.eq("quarter_num", quarterNum);
            queryWrapper.inSql(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
            queryWrapper.eq("pid", 0);
            List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

            for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                PassRade passRade = new PassRade();
                passRade.setSchoolId(schoolQuarter.getSchoolId());
                passRade.setSchoolName(schoolQuarter.getSchoolName());
                passRade.setSchoolPid(schoolQuarter.getPid());
                passRade.setAllShutNum(0);
                passRade.setPlanShutNumPass(0);
                passRade.setNewNumAll(0);
                passRade.setFirstWordCount(0);
                passRade.setFirstRightCount(0);
                passRade.setFirstErrorCount(0);
                passRade.setAgainPassNum(0);
                passRade.setYetPassNum(0);
                passRade.setTrainCount(0);
                passRade.setStudentCount(0);
                passRade.setWordCount(0);
                passRade.setPassDateStudentNum(0);
                passRade.setAgainPassStudentCount(0);
                passRade.setNotFinishStudentNum(0);
                passRade.setAllStudentNum(0);
                passRade.setNotStudentNum(0);
                passRade.setShutnumPassNum(0);
                passRade.setShutnumNotStudyNum(0);
                collectList.add(passRade);
            }

            for (PassRade passRade : collectList) {
                List<PassRade> childRates = new ArrayList<>();
                for (PassRade rade : studyPlanList) {
                    if (passRade.getSchoolId().equals(rade.getSchoolId()) || passRade.getSchoolId().equals(rade.getSchoolPid())) {
                        childRates.add(rade);
                        if (rade.getAllShutNum() != null) {
                            passRade.setAllShutNum(passRade.getAllShutNum() + rade.getAllShutNum());
                        }
                        if (rade.getPlanShutNumPass() != null) {
                            passRade.setPlanShutNumPass(passRade.getPlanShutNumPass() + rade.getPlanShutNumPass());
                        }
                        if (rade.getNewNumAll() != null) {
                            passRade.setNewNumAll(passRade.getNewNumAll() + rade.getNewNumAll());
                        }
                        if (rade.getFirstWordCount() != null) {
                            passRade.setFirstWordCount(passRade.getFirstWordCount() + rade.getFirstWordCount());
                        }
                        if (rade.getFirstRightCount() != null) {
                            passRade.setFirstRightCount(passRade.getFirstRightCount() + rade.getFirstRightCount());
                        }
                        if (rade.getFirstErrorCount() != null) {
                            passRade.setFirstErrorCount(passRade.getFirstErrorCount() + rade.getFirstErrorCount());
                        }
                        if (rade.getAgainPassNum() != null) {
                            passRade.setAgainPassNum(passRade.getAgainPassNum() + rade.getAgainPassNum());
                        }
                        if (rade.getYetPassNum() != null) {
                            passRade.setYetPassNum(passRade.getYetPassNum() + rade.getYetPassNum());
                        }
                        if (rade.getTrainCount() != null) {
                            passRade.setTrainCount(passRade.getTrainCount() + rade.getTrainCount());
                        }
                        if (rade.getStudentCount() != null) {
                            passRade.setStudentCount(passRade.getStudentCount() + rade.getStudentCount());
                        }
                        if (rade.getWordCount() != null) {
                            passRade.setWordCount(passRade.getWordCount() + rade.getWordCount());
                        }
                        if (rade.getPassDateStudentNum() != null) {
                            passRade.setPassDateStudentNum(passRade.getPassDateStudentNum() + rade.getPassDateStudentNum());
                        }
                        if (rade.getAgainPassStudentCount() != null) {
                            passRade.setAgainPassStudentCount(passRade.getAgainPassStudentCount() + rade.getAgainPassStudentCount());
                        }
                        if (rade.getNotFinishStudentNum() != null) {
                            passRade.setNotFinishStudentNum(passRade.getNotFinishStudentNum() + rade.getNotFinishStudentNum());
                        }
                        if (rade.getAllStudentNum() != null) {
                            passRade.setAllStudentNum(passRade.getAllStudentNum() + rade.getAllStudentNum());
                        }
                        if (rade.getNotStudentNum() != null) {
                            passRade.setNotStudentNum(passRade.getNotStudentNum() + rade.getNotStudentNum());
                        }
                        if (rade.getShutnumPassNum() != null) {
                            passRade.setShutnumPassNum(passRade.getShutnumPassNum() + rade.getShutnumPassNum());
                        }
                        if (rade.getShutnumNotStudyNum() != null) {
                            passRade.setShutnumNotStudyNum(passRade.getShutnumNotStudyNum() + rade.getShutnumNotStudyNum());
                        }
                    }
                }
                passRade.setChildRateList(childRates);
            }

            studyPlanList = collectList;
        }

        if (StringUtils.isNotBlank(listState) && listState.equals("1")) {
            for (PassRade passRade : studyPlanList) {
                if (passRade.getGradeId() != null) {
                    passRade.setGradeName(iGradeService.getById(passRade.getGradeId()).getName());
                }
            }
        }

        return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
    }

    @GetMapping("/getMinimumTaskProgressRateIFeign")
    public List<PassRade> getMinimumTaskProgressRateIFeign(
            @RequestParam(value = "yearPart", defaultValue = "", required = false) String yearPart,
            @RequestParam(value = "quarterNum", defaultValue = "", required = false) String quarterNum,
            @RequestParam(value = "yearClassId", defaultValue = "", required = false) String yearClassId,
            @RequestParam(value = "classTypeId", defaultValue = "", required = false) String classTypeId,
            @RequestParam(value = "gradeSection", defaultValue = "", required = false) String gradeSection,
            @RequestParam(value = "type", defaultValue = "1", required = false) Integer type,
            @RequestParam(value = "tabsType", defaultValue = "1", required = false) Integer tabsType,
            @RequestParam(value = "readingStatus", defaultValue = "", required = false) String readingStatus,
            @RequestParam(value = "courseTypeId", defaultValue = "", required = false) String courseTypeId,
            @RequestParam(value = "taskDateStart", defaultValue = "", required = false) String taskDateStart,
            @RequestParam(value = "taskDateEnd", defaultValue = "", required = false) String taskDateEnd,
            @RequestParam(value = "schoolId", defaultValue = "", required = false) String schoolId,
            @RequestParam(value = "include", defaultValue = "", required = false) String include,
            @RequestParam(value = "teamId", defaultValue = "", required = false) String teamId,
            @RequestParam(value = "classify", defaultValue = "", required = false) String classify,
            @RequestParam(value = "trackType", defaultValue = "", required = false) String trackType,
            @RequestParam(value = "taskCourseNumNow", defaultValue = "", required = false) Integer taskCourseNumNow,
            @RequestParam(value = "taskCourseNum", defaultValue = "", required = false) Integer taskCourseNum,
            @RequestParam(value = "teacherId", defaultValue = "", required = false) String teacherId,
            @RequestParam(value = "categoryId", defaultValue = "1", required = false) String categoryId,
            @RequestParam(value = "tactType", required = false) String tactType,
            @RequestParam(value = "teacherName", required = false) String teacherName,
            @RequestParam(value = "studentName", required = false) String studentName,
            @RequestParam(value = "listState", defaultValue = "0", required = false) String listState,// 是否为正确率
            @RequestParam(value = "shutnumType", required = false) String shutnumType,// 0全部，1单词，2专项和试卷
            @RequestParam(value = "isHoliday", defaultValue = "0", required = false) Integer isHoliday, // 0默认，1假期
            @RequestParam(value = "moldType", required = false, defaultValue = "1") String moldType, // 1正常通关率、2到勤通关率
            @RequestParam(value = "isYearClass", required = false, defaultValue = "0") Integer isYearClass // 是否根据年级分组，0否，1是

    ) {

        if (taskCourseNumNow == null || StringUtils.isBlank(taskDateStart) || StringUtils.isBlank(taskDateEnd)) {
            return new ArrayList<>();
        }
        FenAdminData fenAdminData = iAuthAdminContentService.getAdminData(Integer.parseInt(yearPart), Integer.parseInt(quarterNum));
        AuthAdminContent authAdminContent = iAuthAdminContentService.getPermissionForTeacherId(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), fenAdminData.getAdminId(), fenAdminData.getBaseSchoolIdStr());
        LeadDataRs leadDataRs = authAdminContent.getLeadDataRs();

        if (StringUtils.isNotBlank(gradeSection)) {
            //年级段获取出年级
            yearClassId = CommonUtil.getSectionYearClassIds(gradeSection);
        }

        if (StringUtils.isBlank(yearClassId)) {
            yearClassId = authAdminContent.getYearClassIds();
        } else {
            String leadYearClassId = iAuthAdminContentService.getIntersectionForStr(leadDataRs.getYearClassIds(), yearClassId);
            leadDataRs.setYearClassIds(leadYearClassId);

            yearClassId = iAuthAdminContentService.getIntersectionForStr(authAdminContent.getYearClassIds(), yearClassId);
        }

        if (StringUtils.isBlank(schoolId)) {
            schoolId = authAdminContent.getSchoolIds();
        } else {
            String leadSchoolId = iAuthAdminContentService.getIntersectionForStr(leadDataRs.getSchoolIds(), schoolId);
            leadDataRs.setSchoolIds(leadSchoolId);

            schoolId = iAuthAdminContentService.getIntersectionForStr(authAdminContent.getSchoolIds(), schoolId);
        }

        if (StringUtils.isBlank(teacherId)) {
            if (authAdminContent.getIsPerson() == 1) {
                teacherId = String.valueOf(this.adminId);
            }
        }

        List<PassRade> studyPlanList = iStudyPlanService.getMinimumTaskProgressRateNew(Integer.valueOf(readingStatus), schoolId, yearPart, yearClassId, type, classTypeId, quarterNum, gradeSection, taskCourseNum, taskCourseNumNow, courseTypeId, taskDateStart, taskDateEnd, tabsType, include, teamId, classify, trackType, categoryId, teacherId, tactType, listState, teacherName, shutnumType, studentName, isHoliday, moldType, isYearClass, authAdminContent, leadDataRs);

        if (type == 6) {
            List<PassRade> collectList = new ArrayList<>();

            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("year_part", yearPart);
            queryWrapper.eq("quarter_num", quarterNum);
            queryWrapper.inSql(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
            queryWrapper.eq("pid", 0);
            List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

            for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                PassRade passRade = new PassRade();
                passRade.setSchoolId(schoolQuarter.getSchoolId());
                passRade.setSchoolName(schoolQuarter.getSchoolName());
                passRade.setSchoolPid(schoolQuarter.getPid());
                passRade.setAllShutNum(0);
                passRade.setPlanShutNumPass(0);
                passRade.setNewNumAll(0);
                passRade.setFirstWordCount(0);
                passRade.setFirstRightCount(0);
                passRade.setFirstErrorCount(0);
                passRade.setAgainPassNum(0);
                passRade.setYetPassNum(0);
                passRade.setTrainCount(0);
                passRade.setStudentCount(0);
                passRade.setWordCount(0);
                passRade.setPassDateStudentNum(0);
                passRade.setAgainPassStudentCount(0);
                passRade.setNotFinishStudentNum(0);
                passRade.setAllStudentNum(0);
                passRade.setNotStudentNum(0);
                passRade.setShutnumPassNum(0);
                passRade.setShutnumNotStudyNum(0);
                collectList.add(passRade);
            }

            for (PassRade passRade : collectList) {
                List<PassRade> childRates = new ArrayList<>();
                for (PassRade rade : studyPlanList) {
                    if (passRade.getSchoolId().equals(rade.getSchoolId()) || passRade.getSchoolId().equals(rade.getSchoolPid())) {
                        childRates.add(rade);
                        if (rade.getAllShutNum() != null) {
                            passRade.setAllShutNum(passRade.getAllShutNum() + rade.getAllShutNum());
                        }
                        if (rade.getPlanShutNumPass() != null) {
                            passRade.setPlanShutNumPass(passRade.getPlanShutNumPass() + rade.getPlanShutNumPass());
                        }
                        if (rade.getNewNumAll() != null) {
                            passRade.setNewNumAll(passRade.getNewNumAll() + rade.getNewNumAll());
                        }
                        if (rade.getFirstWordCount() != null) {
                            passRade.setFirstWordCount(passRade.getFirstWordCount() + rade.getFirstWordCount());
                        }
                        if (rade.getFirstRightCount() != null) {
                            passRade.setFirstRightCount(passRade.getFirstRightCount() + rade.getFirstRightCount());
                        }
                        if (rade.getFirstErrorCount() != null) {
                            passRade.setFirstErrorCount(passRade.getFirstErrorCount() + rade.getFirstErrorCount());
                        }
                        if (rade.getAgainPassNum() != null) {
                            passRade.setAgainPassNum(passRade.getAgainPassNum() + rade.getAgainPassNum());
                        }
                        if (rade.getYetPassNum() != null) {
                            passRade.setYetPassNum(passRade.getYetPassNum() + rade.getYetPassNum());
                        }
                        if (rade.getTrainCount() != null) {
                            passRade.setTrainCount(passRade.getTrainCount() + rade.getTrainCount());
                        }
                        if (rade.getStudentCount() != null) {
                            passRade.setStudentCount(passRade.getStudentCount() + rade.getStudentCount());
                        }
                        if (rade.getWordCount() != null) {
                            passRade.setWordCount(passRade.getWordCount() + rade.getWordCount());
                        }
                        if (rade.getPassDateStudentNum() != null) {
                            passRade.setPassDateStudentNum(passRade.getPassDateStudentNum() + rade.getPassDateStudentNum());
                        }
                        if (rade.getAgainPassStudentCount() != null) {
                            passRade.setAgainPassStudentCount(passRade.getAgainPassStudentCount() + rade.getAgainPassStudentCount());
                        }
                        if (rade.getNotFinishStudentNum() != null) {
                            passRade.setNotFinishStudentNum(passRade.getNotFinishStudentNum() + rade.getNotFinishStudentNum());
                        }
                        if (rade.getAllStudentNum() != null) {
                            passRade.setAllStudentNum(passRade.getAllStudentNum() + rade.getAllStudentNum());
                        }
                        if (rade.getNotStudentNum() != null) {
                            passRade.setNotStudentNum(passRade.getNotStudentNum() + rade.getNotStudentNum());
                        }
                        if (rade.getShutnumPassNum() != null) {
                            passRade.setShutnumPassNum(passRade.getShutnumPassNum() + rade.getShutnumPassNum());
                        }
                        if (rade.getShutnumNotStudyNum() != null) {
                            passRade.setShutnumNotStudyNum(passRade.getShutnumNotStudyNum() + rade.getShutnumNotStudyNum());
                        }
                    }
                }
                passRade.setChildRateList(childRates);
            }

            studyPlanList = collectList;
        }

        if (StringUtils.isNotBlank(listState) && listState.equals("1")) {
            for (PassRade passRade : studyPlanList) {
                if (passRade.getGradeId() != null) {
                    passRade.setGradeName(iGradeService.getById(passRade.getGradeId()).getName());
                }
            }
        }

        return studyPlanList;
    }

    /**
     * 最低进度通关率-获取升班详情
     *
     * @param reMap
     * @return
     */
    @ApiMethod(value = "home:english:clearance-rate,home:english:accuracy-rate", description = "英语通关率,英语正确率")
    @PostMapping("/getMinimumTaskProgressRateDetail")
    public Result getMinimumTaskProgressRateDetail(@RequestBody Map<String, String> reMap) {
        Integer pageNum = StringUtils.isEmpty(reMap.get("pageNum")) ? 1 : Integer.valueOf(reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty(reMap.get("pageSize")) ? 20 : Integer.valueOf(reMap.get("pageSize"));

        String categoryId = StringUtils.isEmpty(reMap.get("categoryId")) ? "1" : reMap.get("categoryId");
        String schoolId = reMap.get("schoolId");
        String teacherId = reMap.get("teacherId");
        String gradeId = reMap.get("gradeId");
        String yearPart = reMap.get("yearPart");
        String yearClassId = reMap.get("yearClassId");
        String classTypeId = reMap.get("classTypeId");
        // 包含0，不包含1
        String readingStatus = reMap.get("readingStatus");
        // 通关状态 应通关1 已通关2 未通关3 通关4
        String detailType = reMap.get("detailType");
        String subjectsId = "1";
        String quarterNum = reMap.get("quarterNum");
        String gradeSection = reMap.get("gradeSection");
        Integer type = StringUtils.isEmpty(reMap.get("type")) ? 1 : Integer.valueOf(reMap.get("type"));
        // 人数1，关数2
        Integer tabsType = StringUtils.isEmpty(reMap.get("tabsType")) ? 1 : Integer.valueOf(reMap.get("tabsType"));
        Integer isHoliday = StringUtils.isEmpty(reMap.get("isHoliday")) ? 0 : Integer.valueOf(reMap.get("isHoliday"));// 0默认，1假期

        // 开始课次
        Integer taskCourseNum = Integer.parseInt(reMap.get("taskCourseNum"));
        // 结束课次
        Integer taskCourseNumNow = Integer.parseInt(reMap.get("taskCourseNumNow"));
        // 班型
        String courseTypeId = reMap.get("courseTypeId");
        // 开始日期
        String taskDateStart = reMap.get("taskDateStart");
        // 结束日期
        String taskDateEnd = reMap.get("taskDateEnd");
        //0三次课以内，1三次课以外
        String include = reMap.get("include");
        String teamId = reMap.get("teamId");
        String classify = reMap.get("classify");
        String trackType = reMap.get("trackType");
        String studentIdStr = reMap.get("studentIdStr");
        String studentName = reMap.get("studentName");
        String tactType = reMap.get("tactType");// 0全部，1复习检测，2学习检测
        String listState = StringUtils.isEmpty(reMap.get("listState")) ? "0" : reMap.get("listState");// 0通关率，1正确率
        String moldType = StringUtils.isEmpty(reMap.get("moldType")) ? "1" : reMap.get("moldType"); // 1正常通关率、2到勤通关率
        String shutnumType = reMap.get("shutnumType");// 0全部，1单词，2专项和试卷
        Integer isTeam = StringUtils.isEmpty(reMap.get("isTeam")) ? 0 : Integer.valueOf(reMap.get("isTeam")); // 0默认 1战队

        AuthAdminContent authAdminContent = iAuthAdminContentService.getPermissionForTeacherId(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), this.adminId, this.baseSchoolIdStr);
        LeadDataRs leadDataRs = authAdminContent.getLeadDataRs();//个人数据权限

        if (StringUtils.isNotBlank(gradeSection)) {
            //年级段获取出年级
            yearClassId = CommonUtil.getSectionYearClassIds(gradeSection);
        }

        if (StringUtils.isBlank(yearClassId)) {
            yearClassId = authAdminContent.getYearClassIds();
        } else {
            String leadYearClassId = iAuthAdminContentService.getIntersectionForStr(leadDataRs.getYearClassIds(), yearClassId);
            leadDataRs.setYearClassIds(leadYearClassId);

            yearClassId = iAuthAdminContentService.getIntersectionForStr(authAdminContent.getYearClassIds(), yearClassId);
        }

        if (StringUtils.isBlank(schoolId)) {
            schoolId = authAdminContent.getSchoolIds();
        } else {
            String leadSchoolId = iAuthAdminContentService.getIntersectionForStr(leadDataRs.getSchoolIds(), schoolId);
            leadDataRs.setSchoolIds(leadSchoolId);

            schoolId = iAuthAdminContentService.getIntersectionForStr(authAdminContent.getSchoolIds(), schoolId);
        }

        if (StringUtils.isNotBlank(teacherId)) {
            if (!teacherId.equals(String.valueOf(leadDataRs.getTeacherId()))) {
                leadDataRs.setTeacherId(0L);
            }
        }


        List<PassRadeDetail> hashMapList = iStudyPlanService.getMinimumTaskProgressRateDetailNew(Integer.valueOf(readingStatus), schoolId, teacherId, gradeId, yearPart, yearClassId, type, classTypeId, subjectsId, quarterNum, gradeSection, pageNum, pageSize, taskCourseNum, taskCourseNumNow, courseTypeId, taskDateStart, taskDateEnd, detailType, tabsType, include, teamId, classify, trackType, studentIdStr, categoryId, tactType, studentName, listState, shutnumType, isHoliday, moldType, isTeam, authAdminContent, leadDataRs);
        if (isTeam == 0) {
            for (PassRadeDetail passRade : hashMapList) {
                if (passRade.getGradeId() != null) {
                    passRade.setGradeName(iGradeService.getById(passRade.getGradeId()).getName());
                }
            }
        }

//        if (detailType.equals("3")) {
        StringBuilder studentIdSb = new StringBuilder();
        for (PassRadeDetail passRade : hashMapList) {
            studentIdSb.append(passRade.getStudentId()).append(",");
        }

        // studentIdSb去掉最后一个字符串
        if (studentIdSb.length() > 0) {
            Quarter quarter = iQuarterService.getNowQuarter();

            String isBeforQuarter = "1";//是否之前学期1是，0否
            if (quarter != null) {
                //当前学期
                if (yearPart.equals(quarter.getYearPart().toString()) && quarterNum.equals(quarter.getNum().toString())) {
                    isBeforQuarter = "0";

                }
            }

            studentIdSb.deleteCharAt(studentIdSb.length() - 1);
            // 获取学员上课时间
            List<MathChaPlan> mathChaPlanList = iGradeService.getSchoolTimeList(Integer.parseInt(yearPart), Integer.parseInt(quarterNum), readingStatus, isBeforQuarter, studentIdSb.toString());
            for (PassRadeDetail passRade : hashMapList) {
                for (MathChaPlan mathChaPlan : mathChaPlanList) {
                    if (passRade.getStudentId().equals(mathChaPlan.getStudentId())) {
                        passRade.setEnSchoolTime(mathChaPlan.getEnSchoolTime());
                        passRade.setMaSchoolTime(mathChaPlan.getMaSchoolTime());
                        passRade.setPhSchoolTime(mathChaPlan.getPhSchoolTime());
                    }
                }
            }
        }
//        }

        return new Result(Code.OK, new PageResult(hashMapList, (long) hashMapList.size(), 1L));
    }

    /**
     * 停课率 列表
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getSuspensionRate")
    public Result getSuspensionRate(@RequestParam Map<String, String> reMap) {
        List<ArrivalRate> studyPlanList = iStudyPlanService.getSuspensionRate(reMap, this.baseSchoolIdList);
        return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
    }

    /**
     * 停课率 详情
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getSuspensionRateDetail")
    public Result getSuspensionRateDetail(@RequestParam Map<String, String> reMap) {
        List<ArrivalRate> arrivalRateList = iStudyPlanService.getSuspensionRateDetail(reMap);
        return new Result(Code.OK, new PageResult(arrivalRateList, (long) arrivalRateList.size(), 1L));
    }

    /**
     * 任务表 学习到的课次 数字列表显示
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getTaskNumber")
    public Result getTaskNumber(@RequestParam Map<String, String> reMap) {
        EduTask eduTask = new EduTask();
        if (reMap.get("classTypeId") != null && !Objects.equals(reMap.get("classTypeId"), "")) {
            eduTask.setClassTypeId(Long.valueOf(reMap.get("classTypeId")));
        }
        if (reMap.get("yearClassId") != null && !Objects.equals(reMap.get("yearClassId"), "")) {
            eduTask.setYearClassId(Integer.valueOf(reMap.get("yearClassId")));
        }
        if (reMap.get("yearPart") != null && !Objects.equals(reMap.get("yearPart"), "")) {
            eduTask.setYearPart(Integer.valueOf(reMap.get("yearPart")));
        }
        if (reMap.get("quarterNum") != null && !Objects.equals(reMap.get("quarterNum"), "")) {
            eduTask.setQuarterNum(Integer.valueOf(reMap.get("quarterNum")));
        }
        try {
            ArrayList<Object> taskNumber = iStudyPlanService.getTaskNumber(eduTask);
            return new Result(Code.OK, taskNumber);
        } catch (Exception e) {
//            throw new RuntimeException(e);
            return new Result(Code.ERROR, "");
        }
    }

    /**
     * 任务表 学习到的课次 数字、时间列表显示
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getTaskDate")
    public Result getTaskDate(@RequestParam Map<String, String> reMap) {
        EduTask eduTask = new EduTask();
        if (reMap.get("classTypeId") != null && !Objects.equals(reMap.get("classTypeId"), "")) {
            eduTask.setClassTypeId(Long.valueOf(reMap.get("classTypeId")));
        }
        if (reMap.get("yearClassId") != null && !Objects.equals(reMap.get("yearClassId"), "")) {
            eduTask.setYearClassId(Integer.valueOf(reMap.get("yearClassId")));
        }
        if (reMap.get("yearPart") != null && !Objects.equals(reMap.get("yearPart"), "")) {
            eduTask.setYearPart(Integer.valueOf(reMap.get("yearPart")));
        }
        if (reMap.get("quarterNum") != null && !Objects.equals(reMap.get("quarterNum"), "")) {
            eduTask.setQuarterNum(Integer.valueOf(reMap.get("quarterNum")));
        }
        try {
            ArrayList<Object> taskNumber = iStudyPlanService.getTaskDate(eduTask);
            return new Result(Code.OK, taskNumber);
        } catch (Exception e) {
//            throw new RuntimeException(e);
            return new Result(Code.ERROR, "");
        }
    }

    /**
     * 获取当前课次和日期(版本按精品课解锁关数升级后，当前接口暂时不使用)
     */
    @GetMapping("/getTaskNewV1")
    public Result getTaskNewV1(@RequestParam Map<String, String> reMap) {
        String yearPart = reMap.get("yearPart");
        String quarterNum = reMap.get("quarterNum");
        String yearClassId = reMap.get("yearClassId");
        String classTypeId = reMap.get("classTypeId");
        String courseVersionId = reMap.get("courseVersionId");
        String gradeSection = reMap.get("gradeSection");

        if (StringUtils.isNotBlank(gradeSection)) {
            StringBuilder yearClassIdSb = new StringBuilder();
            String[] sections = gradeSection.split(",");
            for (String section : sections) {
                if (section.equals("0")) {
                    yearClassIdSb.append("1,2");
                } else if (section.equals("1")) {
                    yearClassIdSb.append("3,4,5,6");
                } else if (section.equals("2")) {
                    yearClassIdSb.append("7,8,9");
                } else if (section.equals("3")) {
                    yearClassIdSb.append("10,11,12");
                }
                if (!section.equals(sections[sections.length - 1])) {
                    yearClassIdSb.append(",");
                }
            }
            yearClassId = yearClassIdSb.toString();
        }

        Map<String, Object> objectMap = iStudyPlanService.getTaskNew(yearPart, quarterNum, yearClassId, classTypeId, courseVersionId);

        return new Result(Code.OK, objectMap, "");
    }


    /**
     * 获取当前课次和日期
     */
    @GetMapping("/getTaskNew")
    public Result getTaskNew(@RequestParam Map<String, String> reMap) {
        String yearPart = reMap.get("yearPart");
        String quarterNum = reMap.get("quarterNum");
        QueryWrapper<RecordCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("IFNULL(MAX(lesson_num),1) as lesson_num");
        queryWrapper.eq("year_part", yearPart);
        queryWrapper.eq("quarter", quarterNum);
        queryWrapper.eq("subjects_id", 1);
        queryWrapper.eq("status", 1);
        queryWrapper.eq("course_type", 1);
        queryWrapper.eq("lesson_type", 1);
        RecordCourse recordCourse = iRecordCourseService.getOne(queryWrapper);
        Integer maxLessonNum = 1;
        if (!Objects.isNull(recordCourse)) {
            maxLessonNum = recordCourse.getLessonNum();
        }
        Map<String, Object> objectMap = new HashMap<>();
        List<Map<String, Object>> taskList = new ArrayList<>();
        for (int i = 1; i <= maxLessonNum; i++) {
            Map<String, Object> lessonMap = new HashMap<>();
            lessonMap.put("taskCourseNum", i);
            taskList.add(lessonMap);
        }
        objectMap.put("nowTaskNum", maxLessonNum);
        objectMap.put("taskList", taskList);
        return new Result(Code.OK, objectMap, "");

    }

    /**
     * 突破目标通关率
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getBreakShutnumRate")
    public Result getBreakShutnumRate(@RequestParam Map<String, String> reMap,
                                      @RequestParam(value = "schoolId", defaultValue = "", required = false) String schoolId) {

        //Integer type, Integer quarterNum, String startDate, String endDate, String trackId


        String yearPart = reMap.get("yearPart");

        String yearClassId = reMap.get("yearClassId");

        String classTypeId = reMap.get("classTypeId");

        String subjectsId = "1";

        String startDate = null;

        String endDate = null;

        String trackId = null;

        String quarterNum = reMap.get("quarterNum");

        String gradeSection = reMap.get("gradeSection");

        Integer type = 1;
        if (reMap.get("type") != null) {
            type = Integer.parseInt(reMap.get("type"));
        }

        //不传就是原来的关数，传1是人数
        Integer passersStatus = null;
        if (reMap.get("passersStatus") != null && !Objects.equals(reMap.get("passersStatus"), "")) {
            passersStatus = Integer.parseInt(reMap.get("passersStatus"));
        }
        //学习到的课次
        Integer taskCourseNum = null;
        if (reMap.get("taskCourseNum") != null && !Objects.equals(reMap.get("taskCourseNum"), "")) {
            taskCourseNum = Integer.parseInt(reMap.get("taskCourseNum"));
        }

        //学习到的课次
        Integer taskCourseNumNow = null;
        if (reMap.get("taskCourseNumNow") != null && !Objects.equals(reMap.get("taskCourseNumNow"), "")) {
            taskCourseNumNow = Integer.parseInt(reMap.get("taskCourseNumNow"));
        }

        //1突破目标通关率；为空不传就是原来的“基础目标”最低通关率
        Integer taskState = 1;

        //班型
        String courseTypeId = null;
        if (reMap.get("courseTypeId") != null && !Objects.equals(reMap.get("courseTypeId"), "")) {
            courseTypeId = reMap.get("courseTypeId");
        }

        //日期
        String taskDateStart = null;
        if (reMap.get("taskDateStart") != null && !Objects.equals(reMap.get("taskDateStart"), "")) {
            taskDateStart = reMap.get("taskDateStart");
        }
        String taskDateEnd = null;
        if (reMap.get("taskDateEnd") != null && !Objects.equals(reMap.get("taskDateEnd"), "")) {
            taskDateEnd = reMap.get("taskDateEnd");
        }

        if (StringUtils.isBlank(schoolId)) {
            if (type.equals(2)) {
                //校区老师状态，只能看自己所在校区的数据
                schoolId = this.baseSchoolIdList.stream().map(Object::toString).collect(Collectors.joining(","));
            }
        }

        List<StudyPlan> studyPlanList = iStudyPlanService.getMinimumTaskProgressRate(schoolId, yearPart, yearClassId, type, classTypeId, subjectsId, quarterNum, startDate, endDate, trackId, gradeSection, passersStatus, taskCourseNum, taskCourseNumNow, taskState, courseTypeId, taskDateStart, taskDateEnd);
        return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
    }

    /**
     * 突破目标通关率-获取升班详情
     *
     * @param reMap
     * @return
     */
    @PostMapping("/getBreakShutnumRateDetail")
    public Result getBreakShutnumRateDetail(@RequestBody Map<String, String> reMap) {

        //Integer type, Integer quarterNum, String startDate, String endDate, String trackId

        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 20 : Integer.valueOf((String) reMap.get("pageSize"));

        String schoolId = reMap.get("schoolId");

        String teacherId = reMap.get("teacherId");

        String gradeId = reMap.get("gradeId");

        String yearPart = reMap.get("yearPart");

        String yearClassId = reMap.get("yearClassId");

        String classTypeId = reMap.get("classTypeId");

        String subjectsId = "1";

        String startDate = null;

        String endDate = null;

        String trackId = null;

        String quarterNum = reMap.get("quarterNum");

        String gradeSection = reMap.get("gradeSection");

        Integer type = 1;
        if (reMap.get("type") != null) {
            type = Integer.parseInt(reMap.get("type"));
        }

        //不传就是原来的关数，传1是人数
        Integer passersStatus = null;//详情不变，也就是不要传这个参数
//        if(reMap.get("passersStatus") != null && !Objects.equals(reMap.get("passersStatus"), "")){
//            passersStatus = Integer.parseInt(reMap.get("passersStatus"));
//        }
        //学习到的课次
        Integer taskCourseNum = null;
        if (reMap.get("taskCourseNum") != null && !Objects.equals(reMap.get("taskCourseNum"), "")) {
            taskCourseNum = Integer.parseInt(reMap.get("taskCourseNum"));
        }

        Integer taskCourseNumNow = null;
        if (reMap.get("taskCourseNumNow") != null && !Objects.equals(reMap.get("taskCourseNumNow"), "")) {
            taskCourseNumNow = Integer.parseInt(reMap.get("taskCourseNumNow"));
        }

        //1突破目标通关率；为空不传就是原来的“基础目标”最低通关率
        Integer taskState = 1;

        //状态 1已通关；2未通关；空全部
        Integer offState = null;
        if (reMap.get("offState") != null && !Objects.equals(reMap.get("offState"), "")) {
            offState = Integer.parseInt(reMap.get("offState"));
        }

        //班型
        String courseTypeId = null;
        if (reMap.get("courseTypeId") != null && !Objects.equals(reMap.get("courseTypeId"), "")) {
            courseTypeId = reMap.get("courseTypeId");
        }

        //日期
        String taskDateStart = null;
        if (reMap.get("taskDateStart") != null && !Objects.equals(reMap.get("taskDateStart"), "")) {
            taskDateStart = reMap.get("taskDateStart");
        }
        String taskDateEnd = null;
        if (reMap.get("taskDateEnd") != null && !Objects.equals(reMap.get("taskDateEnd"), "")) {
            taskDateEnd = reMap.get("taskDateEnd");
        }

        //新列 用户
        String studentIdString = null;
        if (reMap.get("studentIdString") != null) {
            String studentId = "0";
            if (!Objects.equals(reMap.get("studentIdString"), "")) {
                studentId = reMap.get("studentIdString");
            }
            studentIdString = studentId;
        }

        IPage<StudyPlan> iPage = iStudyPlanService.getMinimumTaskProgressRateDetail(schoolId, teacherId, gradeId, yearPart, yearClassId, type, classTypeId, subjectsId, quarterNum, startDate, endDate, trackId, gradeSection, pageNum, pageSize, passersStatus, taskCourseNum, taskCourseNumNow, taskState, offState, courseTypeId, taskDateStart, taskDateEnd, studentIdString);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    /**
     * 班型
     *
     * @param reMap
     * @return
     */
    @GetMapping("/findForQuarter")
    public Result findForQuarter(@RequestParam Map<String, String> reMap) {
//        Integer quarterNum = Integer.valueOf(reMap.get("quarterNum"));
        try {
            QueryWrapper<CourseType> courseTypeQueryWrapper = new QueryWrapper<>();
            //1假期，0 学期
            /*Integer isVacation = iDictService.getIsVacation(quarterNum);

            if (isVacation.equals(1)) {
                courseTypeQueryWrapper.eq("type", 0).or().eq("type", 2);
            } else {
                courseTypeQueryWrapper.eq("type", 0).or().eq("type", 1);
            }*/
            courseTypeQueryWrapper.eq("type", 0).or().eq("type", 1);
            courseTypeQueryWrapper.orderByAsc("sort");
            CourseType courseType = new CourseType();
            courseType.setId(1565023758494501025L);
            courseType.setName("基础班");
            courseType.setSort(-1);
            List<CourseType> courseTypes = iCourseTypeService.list(courseTypeQueryWrapper);
            courseTypes.add(courseType);//由于精品课不需要基础班，因为在这里添加基础班到队列里

            Collections.sort(courseTypes, Comparator.comparing(CourseType::getSort));

            return new Result(Code.OK, new PageResult(courseTypes, (long) courseTypes.size(), 1L));
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

    @GetMapping("/synchronousStudentIdForClassType")
    @ApiOperation("手动同步用户信息到智能端")
    public Result synchronousStudentIdForClassType() {
        iStudyPlanService.synchronousStudentIdForClassType();
        return new Result(Code.OK, "", "手动同步用户信息到智能端完成");
    }

    @ApiOperation("任务进度")
    @GetMapping("/getTask")
    public Result getTask(@RequestParam(value = "yearPart", defaultValue = "", required = false) String yearPart, @RequestParam(value = "quarterNum", defaultValue = "", required = false) String quarterNum) {
        List<EduTask> tasks = iStudyAdminFeignClient.feignTask(yearPart, quarterNum);

        return new Result(Code.OK, tasks, Code.GET_OK_MSG);
    }

    /**
     * 退费率列表
     */
    @GetMapping("/getReturnPremiumRate")
    public Result getReturnPremiumRate(@RequestParam Map<String, String> reMap) {

        List<ArrivalRate> studyPlanList = iStudyPlanService.getReturnPremiumRate(reMap, this.baseSchoolIdList);
        return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
    }

    /**
     * 退费率详情
     */
    @GetMapping("/getReturnPremiumRateDetail")
    public Result getReturnPremiumRateDetail(@RequestParam Map<String, String> reMap) {
        List<ArrivalRate> arrivalRateList = iStudyPlanService.getReturnPremiumRateDetail(reMap);
        return new Result(Code.OK, new PageResult(arrivalRateList, (long) arrivalRateList.size(), 1L));
    }

    /**
     * 键盘熟练度通关率列表
     */
    @GetMapping("/practiceList")
    public Result getKeyboardPracticeList(@RequestParam(value = "yearPart", defaultValue = "", required = false) Integer yearPart,
                                          @RequestParam(value = "quarterNum", defaultValue = "", required = false) Integer quarterNum,
                                          @RequestParam(value = "type", defaultValue = "1", required = false) Integer type,
                                          @RequestParam(value = "schoolId", defaultValue = "", required = false) String schoolId,
                                          @RequestParam(value = "yearClassId", defaultValue = "", required = false) String yearClassId,
                                          @RequestParam(value = "classTypeId", defaultValue = "", required = false) String classTypeId,
                                          @RequestParam(value = "gradeSection", defaultValue = "", required = false) String gradeSection,
                                          @RequestParam(value = "taskDateStart", defaultValue = "", required = false) String taskDateStart,
                                          @RequestParam(value = "taskDateEnd", defaultValue = "", required = false) String taskDateEnd,
                                          @RequestParam(value = "teamId", defaultValue = "", required = false) String teamId,
                                          @RequestParam(value = "classify", defaultValue = "", required = false) String classify,
                                          @RequestParam(value = "trackType", defaultValue = "", required = false) String trackType
    ) {

        if (StringUtils.isBlank(schoolId)) {
            if (type.equals(2)) {
                //校区老师状态，只能看自己所在校区的数据
                schoolId = this.baseSchoolIdList.stream().map(Object::toString).collect(Collectors.joining(","));
            }
        }

        List<PassRade> studyPlanList = iStudyPlanService.keyboardPracticeList(yearPart, quarterNum, taskDateStart, taskDateEnd, yearClassId, gradeSection, schoolId, classTypeId, type, classify, trackType, teamId);

        if (type == 6 && StringUtils.isBlank(schoolId)) {
            List<PassRade> collectList = new ArrayList<>();

            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("year_part", yearPart);
            queryWrapper.eq("quarter_num", quarterNum);
            queryWrapper.eq("pid", 0);
            List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

            for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                PassRade passRade = new PassRade();
                passRade.setSchoolId(schoolQuarter.getSchoolId());
                passRade.setSchoolName(schoolQuarter.getSchoolName());
                passRade.setSchoolPid(schoolQuarter.getPid());
                passRade.setAllShutNum(0);
                passRade.setPlanShutNumPass(0);
                passRade.setNewNumAll(0);
                collectList.add(passRade);
            }

            for (PassRade passRade : collectList) {
                List<PassRade> childRates = new ArrayList<>();
                for (PassRade rade : studyPlanList) {
                    if (passRade.getSchoolId().equals(rade.getSchoolId()) || passRade.getSchoolId().equals(rade.getSchoolPid())) {
                        childRates.add(rade);
                        if (rade.getAllShutNum() != null) {
                            passRade.setAllShutNum(passRade.getAllShutNum() + rade.getAllShutNum());
                        }
                        if (rade.getPlanShutNumPass() != null) {
                            passRade.setPlanShutNumPass(passRade.getPlanShutNumPass() + rade.getPlanShutNumPass());
                        }
                        if (rade.getNewNumAll() != null) {
                            passRade.setNewNumAll(passRade.getNewNumAll() + rade.getNewNumAll());
                        }
                    }
                }
                passRade.setChildRateList(childRates);
            }

            studyPlanList = collectList;
        } else if (type == 2 || type == 4) {
            for (PassRade passRade : studyPlanList) {
                passRade.setSchoolName(iOrganizationService.getById(passRade.getSchoolId()).getName());
            }
        }

        return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
    }

    /**
     * 键盘熟练度通关率详情
     */
    @GetMapping("/practiceDetails")
    public Result getKeyboardPracticeDetails(@RequestParam(value = "yearPart", defaultValue = "", required = false) Integer yearPart,
                                             @RequestParam(value = "quarterNum", defaultValue = "", required = false) Integer quarterNum,
                                             @RequestParam(value = "type", defaultValue = "1", required = false) Integer type,
                                             @RequestParam(value = "detailType", defaultValue = "", required = false) String detailType,//0全部 1已通 2未通 3通关
                                             @RequestParam(value = "teacherId", defaultValue = "", required = false) String teacherId,
                                             @RequestParam(value = "schoolId", defaultValue = "", required = false) String schoolId,
                                             @RequestParam(value = "gradeId", defaultValue = "", required = false) String gradeId,
                                             @RequestParam(value = "studentIdList", defaultValue = "", required = false) String studentIdList,
                                             @RequestParam(value = "yearClassId", defaultValue = "", required = false) String yearClassId,
                                             @RequestParam(value = "classTypeId", defaultValue = "", required = false) String classTypeId,
                                             @RequestParam(value = "gradeSection", defaultValue = "", required = false) String gradeSection,
                                             @RequestParam(value = "taskDateStart", defaultValue = "", required = false) String taskDateStart,
                                             @RequestParam(value = "taskDateEnd", defaultValue = "", required = false) String taskDateEnd,
                                             @RequestParam(value = "teamId", defaultValue = "", required = false) String teamId,
                                             @RequestParam(value = "classify", defaultValue = "", required = false) String classify,
                                             @RequestParam(value = "trackType", defaultValue = "", required = false) String trackType
    ) {

        List<PassRadeDetail> studyPlanList = iStudyPlanService.keyboardPracticeDetails(yearPart, quarterNum, taskDateStart, taskDateEnd, yearClassId, gradeSection, schoolId, classTypeId, teacherId, type, gradeId, detailType, studentIdList, classify, trackType, teamId);

        return new Result(Code.OK, new PageResult(studyPlanList, (long) studyPlanList.size(), 1L));
    }

}
